<script>
	export default {
		globalData: {
			openid: '',
		},
		onLaunch: async function() {
			const storageData = uni.getStorageSync('loginInfo')
			if (storageData) {
				this.globalData.openid = JSON.parse(storageData).openid
			} else {
				uni.login({
					provider: 'weixin', //使用微信登录
					success: (loginRes) => {
						uni.request({
							url: 'https://api.weixin.qq.com/sns/jscode2session',
							data: {
								appid: 'wx2ee954f54dc71599',
								secret: 'ba6aa60ef7f7c5b1e7dc3ad8b1507b6c',
								js_code: loginRes.code,
								grant_type: 'authorization_code'
							},
							success: (res) => {
								console.log(res.data, '小程序登录');
								const {
									openid,
									session_key
								} = res.data
								this.globalData.openid = openid
								uni.setStorageSync('loginInfo', JSON.stringify({
									openid,
									session_key
								}))
							},
							fail: () => {
								uni.showToast({
									title: '网络错误，请联系开发者或稍后重试',
									icon: 'error'
								})
							}
						})
					}
				})
			}

			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	// 字体图标样式
	@import '@/static/iconfonts/iconfont.css';

	// 设置整个项目的背景色
	page {
		background-color: rgba(98, 101, 242, .1);
		--theme-color: #6265f2; // 主题色
		font-size: 28rpx;
		line-height: 1;
	}

	image {
		width: 100%;
		height: 100%;
	}

	button,
	view,
	text,
	image,
	input {
		margin: 0;
		padding: 0;
	}

	button::after {
		border: none;
	}

	.image-circle {
		display: block;
		border-radius: 50%;
	}

	.display-flex {
		display: flex;
		align-items: center;
	}
</style>